package com.leoblog.test.dao;

import com.leoblog.dao.impl.CategoryDao;
import com.leoblog.dao.impl.UserDao;
import com.leoblog.dao.impl.WebSiteDao;
import com.leoblog.domain.Category;
import com.leoblog.domain.User;
import com.leoblog.domain.WebSite;
import com.leoblog.test.CategoryMother;
import com.leoblog.test.UserMother;
import com.leoblog.test.WebSiteMother;
import com.leoblog.test.base.AbstractUTestBaseSpringDaoTestCase;

public class UTestCategoryDao extends AbstractUTestBaseSpringDaoTestCase{
	/**
	 * category dao
	 */
	private CategoryDao categoryDao;

	private WebSiteDao webSiteDao;
	
	private UserDao userDao;
	
	public WebSiteDao getWebSiteDao() {
		return webSiteDao;
	}

	public void setWebSiteDao(WebSiteDao webSiteDao) {
		this.webSiteDao = webSiteDao;
	}

	public CategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}
	
	public void testAddCategory() throws Exception{
		
		WebSite webSite = this.createWebsite();
		
		Category category1 = CategoryMother.newInstance();
		category1.setWebSite(webSite);
		
		Category category2 = CategoryMother.newInstance();
		category2.setWebSite(webSite);
		category2.setParent(category1);
		
		categoryDao.save(category1);
		categoryDao.save(category2);
		
		Category loadedCategory1 = categoryDao.get(category1.getId());
		Category loadedCategory2 = categoryDao.get(category2.getId());
		
		assertNotNull(loadedCategory1.getId());
		assertNotNull(loadedCategory2.getId());
		
		assertNotNull(loadedCategory1.getWebSite());
		assertNotNull(loadedCategory2.getWebSite());
		
		assertNotNull(loadedCategory2.getParent());
		
		assertEquals(category1.getName(), loadedCategory1.getName());
		assertEquals(category2.getName(), loadedCategory2.getName());
		assertEquals(loadedCategory1, loadedCategory2.getParent());
	}
	
	public void testUpdateCategory()throws Exception{
		
		WebSite webSite = this.createWebsite();
		Category category = CategoryMother.newInstance();
		category.setWebSite(webSite);
		//create a instance in database
		categoryDao.save(category);
		//load it
		Category loadedCategory = categoryDao.get(category.getId());
		//update it
		loadedCategory.setName("updatedCategoryName");
		categoryDao.save(loadedCategory);
		
		Category loadedAgainCategory = categoryDao.get(category.getId());
		
		assertNotNull(loadedAgainCategory);
		assertEquals("updatedCategoryName", loadedAgainCategory.getName());
		assertEquals(category.getWebSite().getId(), loadedAgainCategory.getWebSite().getId());
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	/**
	 * create website for a category instance
	 * createWebsite, Create on Aug 27, 2008
	 * @author<a href="mailto:nicholas85211@gmail.com">Nicholas</a>
	 * @return WebSite
	 */
	private WebSite createWebsite(){
		WebSite webSite = WebSiteMother.newInstance();
		
		User author = UserMother.newInstance();
		
		userDao.save(author);
		
		webSite.setUser(author);
		
		webSiteDao.save(webSite);
		
		return webSite;
	}

}
